<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class HouseAttr extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'house_attr';
    public static function getHouseattrData($parentFiled)
    {
        //存储处理后的房源属性
        $houseattr = [];
        //判断是否为房源属性id
        if(is_numeric($parentFiled) && $parentFiled!==0){
            //查询房源属性信息
            $filedInfo = self::find($parentFiled);
            return $filedInfo->name;
        }
        //判断是否查询顶级
        if($parentFiled!==0){
            //非 查询该顶级的id
            $parentFiled = self::where('filed_name',$parentFiled)->get()->pluck('id');
        }else{
            $houseattr[0] = '顶级';
        }
        //查询所有顶级或该字段的子级
        $houseattrData = self::where('pid',$parentFiled)->get();
        //处理对象
        foreach ($houseattrData as $v) {
            $houseattr[$v->id] = $v->name;
        }

        return $houseattr;
    }
    public function setHouseLabelAttribute($value)
    {
        $this->attributes['house_label'] = implode(',',$value);
    }

}
